package com.tianqu.tms.dao;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import com.tianqu.tms.core.exception.TmsDaoException;
import com.tianqu.tms.dto.MarketCarServiceDto;

/**
 * @author mufeiyu
 * 库存车辆业务表
 */
public class MarketCarServiceDaoImp extends TmsDao implements MarketCarServiceDao {

	protected MarketCarServiceDaoImp() throws TmsDaoException {
		super();
	}
	public void updateCarServiceInfo(MarketCarServiceDto dto)throws TmsDaoException{
		PreparedStatement ps = null;
		ResultSet rs = null;
		StringBuffer sql = new StringBuffer();
		sql.append("UPDATE ");
		sql.append(TmsTableNames.TMS_MARKET_CAR_SERVICE );
		sql.append(" SET ");
		sql.append(" CHECKIMGPATH=?,");
		sql.append(" CHECKSTATUS=?,");
		sql.append(" CHECKUSER=?,");
		sql.append(" CHECKTIME=?,");		
		sql.append(" CHECKVALIDTIME=?,");		
		sql.append(" ISVALID=? ");
		sql.append(" WHERE");
		sql.append(" SERIAL=?");
		try {
			ps = conn.prepareStatement(sql.toString());
			ps.setString(1, dto.getCheckImgPath());
			ps.setInt(2, dto.getCheckStatus());
			ps.setString(3, dto.getCheckUser());
			ps.setTimestamp(4, dto.getCheckTime());
			ps.setTimestamp(5, dto.getCheckValidTime());
			ps.setInt(6, dto.getIsValid());
			ps.setString(7, dto.getSerial());
			
			ps.executeUpdate();
		} catch (SQLException e) {
			throw new TmsDaoException(e.toString());
		} finally {
			close(rs, ps);
		}
	}

	@Override
	public void updateCarServiceInfoNew(MarketCarServiceDto dto) throws TmsDaoException {
		PreparedStatement ps = null;
		ResultSet rs = null;
		StringBuffer sql = new StringBuffer();
		sql.append("UPDATE ");
		sql.append(TmsTableNames.TMS_MARKET_CAR_SERVICE );
		sql.append(" SET ");
		sql.append(" CHECKIMGPATH=?,");
		sql.append(" CHECKSTATUS=?,");
		sql.append(" CHECKUSER=?,");
		sql.append(" CHECKTIME=?,");
		sql.append(" CHECKVALIDTIME=?,");
		sql.append(" ISVALID=?, ");
		sql.append(" CHECKPRICE=?, ");
		sql.append(" TRANSFERPRICE=?, ");
		sql.append(" ISREBATE=? ");
		sql.append(" WHERE");
		sql.append(" SERIAL=?");
		try {
			ps = conn.prepareStatement(sql.toString());
			ps.setString(1, dto.getCheckImgPath());
			ps.setInt(2, dto.getCheckStatus());
			ps.setString(3, dto.getCheckUser());
			ps.setTimestamp(4, dto.getCheckTime());
			ps.setTimestamp(5, dto.getCheckValidTime());
			ps.setInt(6, dto.getIsValid());
			ps.setInt(7, dto.getCheckPrice());
			ps.setInt(8, dto.getTransferPrice());
			ps.setInt(9, dto.getIsRebate());
			ps.setString(10, dto.getSerial());

			ps.executeUpdate();
		} catch (SQLException e) {
			throw new TmsDaoException(e.toString());
		} finally {
			close(rs, ps);
		}
	}

	public void updateIsRebate(int isRebate,String serial)throws TmsDaoException{
		PreparedStatement ps = null;
		ResultSet rs = null;
		StringBuffer sql = new StringBuffer();
		sql.append("UPDATE ");
		sql.append(TmsTableNames.TMS_MARKET_CAR_SERVICE );
		sql.append(" SET ");
		sql.append(" ISREBATE=?");
		sql.append(" WHERE");
		sql.append(" SERIAL=?");
		try {
			ps = conn.prepareStatement(sql.toString());
			ps.setInt(1, isRebate);
			ps.setString(2, serial);
			ps.executeUpdate();
		} catch (SQLException e) {
			throw new TmsDaoException(e.toString());
		} finally {
			close(rs, ps);
		}
	}
	
	public void updateCarServiceAllInfo(MarketCarServiceDto dto)throws TmsDaoException{
		PreparedStatement ps = null;
		ResultSet rs = null;
		StringBuffer sql = new StringBuffer();
		sql.append("UPDATE ");
		sql.append(TmsTableNames.TMS_MARKET_CAR_SERVICE );
		sql.append(" SET ");
		sql.append(" CHECKSTATUS=?,");
		sql.append(" CHECKIMGPATH=?,");	
		sql.append(" CHECKUSER=?,");
		sql.append(" CHECKTIME=?,");		
		sql.append(" CHECKVALIDTIME=?,");		
		sql.append(" ISVALID=?, ");
		sql.append(" CHECKPRICE=?, ");
		if(dto.getIsRebate() >=0) sql.append(" ISREBATE="+dto.getIsRebate()+", ");
		sql.append(" TRANSFERPRICE=? ");
		sql.append(" WHERE");
		sql.append(" SERIAL=?");
		try {
			ps = conn.prepareStatement(sql.toString());
			ps.setInt(1, dto.getCheckStatus());
			ps.setString(2, dto.getCheckImgPath());
			ps.setString(3, dto.getCheckUser());
			ps.setTimestamp(4, dto.getCheckTime());
			ps.setTimestamp(5, dto.getCheckValidTime());
			ps.setInt(6, dto.getIsValid());
			ps.setInt(7, dto.getCheckPrice());
			ps.setInt(8, dto.getTransferPrice());
			ps.setString(9, dto.getSerial());
			
			ps.executeUpdate();
		} catch (SQLException e) {
			throw new TmsDaoException(e.toString());
		} finally {
			close(rs, ps);
		}
	}
	public boolean isExistCarServiceInfoBySerial(String serial)throws TmsDaoException{
		PreparedStatement ps = null;
		ResultSet rs = null;
		boolean result = false;
		StringBuffer sql = new StringBuffer("SELECT ");
		sql.append(" ID ");
		sql.append(" FROM ");
		sql.append(TmsTableNames.TMS_MARKET_CAR_SERVICE);
		sql.append(" WHERE ");
		sql.append(" SERIAL=? ");
		try {
			ps = conn.prepareStatement(sql.toString());
			ps.setString(1, serial);
			rs = ps.executeQuery();
			while(rs.next()){
				result = true;
			}
			return result;
		} catch (SQLException e) {
			throw new TmsDaoException(e.toString());

		} finally {
			super.close(rs, ps);
		}
	}
	public void insertCarServiceInfo(MarketCarServiceDto dto)throws TmsDaoException{
		PreparedStatement ps = null;
		ResultSet rs = null;
		try {
			StringBuffer sql = new StringBuffer("INSERT INTO ");
			sql.append(TmsTableNames.TMS_MARKET_CAR_SERVICE);
			sql.append("(");		
			sql.append(" SERIAL, ");
			sql.append(" CHECKIMGPATH, ");
			sql.append(" CHECKSTATUS, ");
			sql.append(" CHECKUSER, ");
			sql.append(" CHECKTIME, ");		
			sql.append(" CHECKVALIDTIME, ");		
			sql.append(" ISVALID, ");
			sql.append(" CHECKPRICE ");
			sql.append("  ) VALUES ( ");
			sql.append(" ?,?,?,?,?,?,?,0 )");

			ps = conn.prepareStatement(sql.toString());
			
			ps.setString(1, dto.getSerial());
			ps.setString(2, dto.getCheckImgPath());
			ps.setInt(3, dto.getCheckStatus());
			ps.setString(4, dto.getCheckUser());
			ps.setTimestamp(5, dto.getCheckTime());
			ps.setTimestamp(6, dto.getCheckValidTime());
			ps.setInt(7, dto.getIsValid());
			
			ps.executeUpdate();
			
		} catch (SQLException e) {
			throw new TmsDaoException(e.toString());

		} finally {
			super.close(rs, ps);
		}
	}
	public MarketCarServiceDto findCarServiceInfoBySerial(String serial)throws TmsDaoException{
		PreparedStatement ps = null;
		ResultSet rs = null;
		MarketCarServiceDto dto = null;
		StringBuffer sql = new StringBuffer("SELECT ");
		sql.append(" SERIAL, ");
		sql.append(" CHECKIMGPATH, ");
		sql.append(" CHECKSTATUS, ");
		sql.append(" CHECKUSER, ");
		sql.append(" CHECKTIME, ");		
		sql.append(" CHECKVALIDTIME, ");		
		sql.append(" ISVALID, ");
		sql.append(" ISREBATE, ");
		sql.append(" CHECKPRICE, ");
		sql.append(" TRANSFERPRICE ");
		sql.append(" FROM ");
		sql.append(TmsTableNames.TMS_MARKET_CAR_SERVICE);
		sql.append(" WHERE ");
		sql.append(" SERIAL=? ");
		try {
			ps = conn.prepareStatement(sql.toString());
			ps.setString(1, serial);
			rs = ps.executeQuery();
			while(rs.next()){
				dto = new MarketCarServiceDto();
				dto.setSerial(rs.getString("SERIAL"));
				dto.setCheckImgPath(rs.getString("CHECKIMGPATH"));
				dto.setCheckStatus(rs.getInt("CHECKSTATUS"));
				dto.setCheckUser(rs.getString("CHECKUSER"));
				dto.setCheckTime(rs.getTimestamp("CHECKTIME"));
				dto.setCheckValidTime(rs.getTimestamp("CHECKVALIDTIME"));
				dto.setIsValid(rs.getInt("ISVALID"));
				dto.setCheckPrice(rs.getInt("CHECKPRICE"));
				dto.setTransferPrice(rs.getInt("TRANSFERPRICE"));
				dto.setIsRebate(rs.getInt("ISREBATE"));
			}
			return dto;
		} catch (SQLException e) {
			throw new TmsDaoException(e.toString());

		} finally {
			super.close(rs, ps);
		}
	}
	public void insertCarServiceIsValid(MarketCarServiceDto dto)throws TmsDaoException{
		PreparedStatement ps = null;
		ResultSet rs = null;
		try {
			StringBuffer sql = new StringBuffer("INSERT INTO ");
			sql.append(TmsTableNames.TMS_MARKET_CAR_SERVICE);
			sql.append("(");		
			sql.append(" SERIAL, ");		
			sql.append(" ISVALID, ");
			sql.append(" CHECKSTATUS ");
			sql.append("  ) VALUES ( ");
			sql.append(" ?,?,? )");

			ps = conn.prepareStatement(sql.toString());
			
			ps.setString(1, dto.getSerial());
			ps.setInt(2, dto.getIsValid());
			ps.setInt(3, dto.getCheckStatus());
			
			ps.executeUpdate();
			
		} catch (SQLException e) {
			throw new TmsDaoException(e.toString());

		} finally {
			super.close(rs, ps);
		}
	}
	public void updateCarServicePrice(MarketCarServiceDto dto)throws TmsDaoException{
		PreparedStatement ps = null;
		ResultSet rs = null;
		StringBuffer sql = new StringBuffer();
		sql.append("UPDATE ");
		sql.append(TmsTableNames.TMS_MARKET_CAR_SERVICE );
		sql.append(" SET ");		
		sql.append(" CHECKPRICE=?, ");
		sql.append(" TRANSFERPRICE=?, ");
		sql.append(" CHECKPRICE=1, ");
		sql.append(" ISREBATE=2 ");//未返利
		sql.append(" WHERE");
		sql.append(" SERIAL=? ");
		try {
			ps = conn.prepareStatement(sql.toString());
			
			ps.setInt(1, dto.getCheckPrice());
			ps.setInt(2, dto.getTransferPrice());
			ps.setString(3, dto.getSerial());
			
			ps.executeUpdate();
		} catch (SQLException e) {
			throw new TmsDaoException(e.toString());
		} finally {
			close(rs, ps);
		}
	}
}
